<template>
	<div>
		<el-table :data="tableData" stripe style="width: 100%">
			<el-table-column prop="orderBy" label="序号" width="50">
				<template slot-scope="scope">{{ scope.row.orderBy || 10 }}</template>
			</el-table-column>
			<el-table-column prop="name" label="用户">
				<template slot-scope="scope">{{ scope.row.sysUser | showUserInfo }}</template>
			</el-table-column>
			<el-table-column prop="auditStatus" label="状态">
				<template slot-scope="scope">
					<span v-show="scope.row.auditStatus == 1">待审核</span>
					<span v-show="scope.row.auditStatus == 2">审核通过</span>
					<span v-show="scope.row.auditStatus == 3">不通过</span>
				</template>
			</el-table-column>
			<el-table-column prop="des" label="申请描述">
				<template slot-scope="scope">{{ scope.row.des || '--' }}</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间"></el-table-column>
			<el-table-column label="操作" width="190">
				<template slot-scope="{ row }">
					<el-button-group>
						<el-button v-if="row.auditStatus == 1" type="primary" size="mini" @click="handleAudit(row)">审核</el-button>
						<!-- <el-button
                            v-if="row.sysUser.del == 2"
                            type="primary"
                            size="mini"
                            @click="handleBuild(row)"
                        >授权共建</el-button> -->
						<el-button type="primary" size="mini" @click="handleSort(row)">排序</el-button>
						<el-button type="danger" size="mini" @click="handleDelete(row)">删除</el-button>
					</el-button-group>
				</template>
			</el-table-column>
			<div slot="empty" class="emptyBg">
				<el-empty :image-size="200"></el-empty>
			</div>
		</el-table>
		<Pagination
			v-show="parseInt(pageObj.count) > 0"
			:total="parseInt(pageObj.count)"
			@change="getList"
			:page.sync="listQuery.page"
			:limit.sync="listQuery.limit"
		/>
		<AddTeacher @e-userData="handleAdd" />
		<Audit />
		<Build />
		<Sort />
		<UserSelect ref="userSelect" :initialValue="initialValue" @e-userData="handleAdd" />
	</div>
</template>
<script>
import { sysCourseStudentQuery, sysCourseStudentDelete, sysCourseStudentAdd } from '@/api/admin/courseLibrary/sysCourseStudent.js';
import Bus from '@/utils/bus.js';
export default {
	components: {
		AddTeacher: () => import('./AddTeacher.vue'),
		Audit: () => import('./Audit.vue'),
		Build: () => import('./Build.vue'),
		Sort: () => import('./Sort.vue'),
		UserSelect: () => import('@/components/userSelect')
	},
	data() {
		return {
			listQuery: {
				page: 1,
				limit: 10,
				courseId: this.$store.getters.courseId
			},
			tableData: [],
			pageObj: {},
			initialValue: {
				userId: []
			}
		};
	},
	mounted() {
		this.getList();
		Bus.$on('refreshCourseTeam', (v) => {
			if (v) {
				Object.assign(this.listQuery, v);
			}
			this.getList();
		});
	},
	beforeDestroy() {
		Bus.$off('refreshCourseTeam');
	},
	methods: {
		getList() {
			sysCourseStudentQuery(this.listQuery).then((res) => {
				this.tableData = res.data.data;
				this.tableData = this.tableData.filter((v) => v.hasOwnProperty('sysUser'));
				this.pageObj = res.data;
				this.pageObj.count = this.tableData.length;
				this.initialValue.userId = this.tableData.map((v) => v.studentId);
			});
		},
		//审核
		handleAudit(row) {
			Bus.$emit('showDialogCourseTeamAudit', row);
		},
		//授权共建
		handleBuild(row) {
			Bus.$emit('showDialogCourseBuildAudit', row);
		},
		//排序
		handleSort(row) {
			Bus.$emit('showDialogCourseTeamSort', row);
		},
		//删除确认
		handleDelete(row) {
			this.$confirm('确定要删除吗?', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.delete(row);
				})
				.catch(() => {});
		},
		//删除
		delete(row) {
			sysCourseStudentDelete(row.id).then((res) => {
				this.getList();
				this.$message({
					message: '操作成功',
					type: 'success'
				});
			});
		},
		handleAdd(v) {
			if (!v.userId.length) return;
			var form = {
				studentId: '',
				auditStatus: '2',
				orderBy: '10',
				courseId: this.$store.getters.courseId,
				courseOriginator: this.$store.getters.courseCreateBy
			};
			console.log(this.$store.getters.courseCreateBy);
			return false;
			form.studentId = v.userId.map((v) => v.id).toString();
			sysCourseStudentAdd(form).then((res) => {
				this.getList();
				this.$message({
					message: '操作成功',
					type: 'success'
				});
			});
		}
	}
};
</script>
